package org.ticlib.taskwrapper
{
	import flash.events.Event;
	
	import mx.rpc.events.FaultEvent;
	
	import org.ticlib.task.ParalelTask;
	import org.ticlib.task.Task;

	public class ParalelWrapper extends TaskWrapper
	{
		public static function add(wrapper:TaskWrapper):TaskWrapper
		{
			var paralel:ParalelWrapper = new ParalelWrapper();
			paralel.add(wrapper);
			return paralel;
		}
		
		public function ParalelWrapper(id:String = "")
		{
			super(id)
			task = new ParalelTask();
			task.addEventListener(Event.COMPLETE, onComplete);
			task.addEventListener(FaultEvent.FAULT, onFault);
			task.addEventListener(Event.CANCEL, onCancel);
			task.addEventListener(Event.CONNECT, onConnect);
		}

		
		private function onComplete(e:Event):void
		{
			resultFunction.call(this, (e.target as Task).resultObject, uniqueId);
		}
		
		private function onFault(e:Event):void
		{
			faultFunction.call(this, (e.target as Task).resultObject, uniqueId);
		}
		
		private function onCancel(e:Event):void
		{
			cancelFunction.call(this, (e.target as Task).resultObject, uniqueId);
		}

		private function onConnect(e:Event):void
		{
			connectFunction.call(this, (e.target as Task).resultObject, uniqueId);
		}

		override public function add(wrapper:TaskWrapper):TaskWrapper
		{
			super.add(wrapper);
			(task as ParalelTask).addTask(wrapper.task);
			return this;
		}
		
		override public function execute():void
		{
			task.execute();
		}
	}
}